app.controller('BusShiftTplStaionModalCtrl', function ($scope, $modalInstance, param, ajax, $modal, toaster) {

	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.param = param;
	if ($scope.param.data == null) $scope.param.data = {};
	if ($scope.param.data.stationFilter != null) $scope.filter = angular.fromJson($scope.param.data.stationFilter);
	$scope.ids = [];

	//获取站点列表
	$scope.loadStationList = function () {
		ajax.call(new Req.GET({
			resource: 'route/bs/' + $scope.param.data.route.id + '/station'
		})).then(function (result) {
			$scope.list = result.result;
			if ($scope.param.data.stationFilter != null) {
				for (var i = 0; i < $scope.list.length; i++) {
					if ($scope.filter.indexOf($scope.list[i].id) < 0) {
						$scope.list[i].disabled = false;
					} else {
						$scope.list[i].disabled = true;
					}
				}
			}
		}, function () { });
	};
	$scope.loadStationList();

	$scope.shiftSelect = function () {
		return $modal.open({
			templateUrl: 'template/bus/shiftTemplate/updateShift.html?v=f9b9b5f55b',
			controller: 'BusShiftTplUpdateCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"list": $scope.shiftList
					};
				}
			}
		});
	};

	$scope.sendRequest = function (station, datalist) {
		var disable = !station.disabled;
		var action = disable ? '屏蔽站点' : '取消屏蔽站点';

		updater.datalist = datalist || [];
		updater.id = $scope.param.data.id;
		updater.resource = 'shift/bs/tpl/station/' + station.id + '/' + $scope.param.data.id + '/' + (disable ? 'ban' : 'ok');
		ajax.call(updater).then(function (result) {
			if (result.code == 200) {
				if (datalist && datalist.length > 0) toaster.pop('success', '成功', action + "成功。");
				else toaster.pop('success', '成功', action + '成功，没有要同步的发班计划。');
				station.disabled = disable;
			} else {
				toaster.pop('error', '错误', action + '失败！');
			}
		}, $scope.serverError);
	};

	$scope.switch = function (station) {
		if ($scope.myForm.$valid) {
			ajax.call(new Req.GET({
				resource: 'shift/bs/' + $scope.param.data.id + '/list'
			})).then(function (result) {
				$scope.shiftList = result.result;
				if ($scope.shiftList.length > 0) {
					$scope.shiftSelect().result.then(function (param) {
						$scope.sendRequest(station, param);
					});
				} else {
					$scope.sendRequest(station);
				}
			}, function () { });
		}
	};

	$scope.cancel = function () {
		$modalInstance.close(null);
	};
});